﻿Public Class DateRange
	Inherits ComboBox

	Dim m_UseEnterAsTab As Boolean

	Sub New()
		Value = ""
	End Sub

	Public Overrides Property Value() As Object
		Get
			Dim ControlValue As String = (Mid(Text, 7, 4) & "." & Mid(Text, 4, 2) & "." & Mid(Text, 1, 2))
			If Not IsDate(ControlValue) Then Return DBNull.Value
			Return CDate(ControlValue)
		End Get
		Set(ByVal Value As Object)
			If IsDBNull(Value) Then Value = ""
			If IsDate(Value) Then
				Dim Sep As String = Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.DateSeparator
				Text = Replace(String.Format(Value, "dd/MM/yyyy"), Sep, "/")
			ElseIf Len(Value) = 8 Then
				Text = Mid(Value, 7, 2) & "/" & Mid(Value, 5, 2) & "/" & Mid(Value, 1, 4)
			ElseIf Len(Value) = 10 Then
				Text = Mid(Value, 1, 2) & "/" & Mid(Value, 4, 2) & "/" & Mid(Value, 7, 4)
			Else
				Text = "__/__/____"
			End If
		End Set
	End Property

	Private Sub DateBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.TextChanged
		RaiseChangeEvent()
	End Sub

	Private Sub DateBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
		Select Case e.KeyCode
			Case Keys.Left
				FocusDateChar(SelectionStart, -1)
				e.Handled = True
			Case Keys.Right
				FocusDateChar(SelectionStart, 1)
				e.Handled = True
			Case Keys.Back
				If Me.ReadOnly Then Exit Sub
				If SelectionLength > 1 Then
					Mid(Text, SelectionStart + 1, SelectionLength) = Mid("__/__/_____", SelectionStart + 1, SelectionLength)
				Else
					FocusDateChar(SelectionStart, -1)
					SelectedText = "_"
					FocusDateChar(SelectionStart, -1)
				End If
				e.Handled = True
			Case Keys.Delete
				If Me.ReadOnly Then Exit Sub
				If SelectionLength > 0 Then
					Mid(Text, SelectionStart + 1, SelectionLength) = Mid("__/__/_____", SelectionStart + 1, SelectionLength)
				Else
					FocusDateChar(SelectionStart, 0)
					SelectedText = "_"
					FocusDateChar(SelectionStart - 1, 0)
				End If
				e.Handled = True
		End Select
	End Sub

	Protected Overrides Sub OnKeyPress(e As System.Windows.Forms.KeyPressEventArgs)
		MyBase.OnKeyPress(e)
		If e.Handled Then Return
		If Me.ReadOnly Then Exit Sub
		If InStr("0123456789", e.KeyChar) > 0 Then
			FocusDateChar(SelectionStart, 0)
			If SelectedText <> "" Then SelectedText = e.KeyChar
			FocusDateChar(SelectionStart, 0)
		End If
		e.Handled = True
	End Sub

	Private Sub FocusDateChar(ByVal Index, ByVal Offset)
		Index = Index + Offset
		If Index < 0 Then Exit Sub
		If Index > 10 Then Exit Sub
		If Offset >= 0 Then
			SelectionStart = Choose(Index + 1, 0, 1, 3, 3, 4, 6, 6, 7, 8, 9, 10)
		Else
			SelectionStart = Choose(Index + 1, 0, 1, 1, 3, 4, 4, 6, 7, 8, 9, 10)
		End If
		SelectionLength = 1
	End Sub

	Public Overrides ReadOnly Property IsEmpty() As Boolean
		Get
			If Text = "__/__/____" Then Return True
			If Not IsValid Then Return True
		End Get
	End Property

	ReadOnly Property IsValid() As Boolean
		Get
			Dim ControlValue As String = (Mid(Text, 7, 4) & "." & Mid(Text, 4, 2) & "." & Mid(Text, 1, 2))
			If Not IsDate(ControlValue) Then Return False
			Return True
		End Get
	End Property

	Public Overrides Function Errors() As String
		If IsRequired AndAlso IsEmpty Then Return Caption & " alanı boş olamaz."
		If Not IsEmpty AndAlso Not IsValid Then Return Caption & " alanı geçersiz bir tarih içeriyor."
		Return ""
	End Function

	Public Overrides Function GetDropDownControl() As System.Windows.Forms.Control
		Dim Calendar As New Calendar
		If Not IsDBNull(Value) Then Calendar.Value = Value
		AddHandler Calendar.DateSelected, AddressOf SelectDate
		Return Calendar
	End Function

	Sub SelectDate(sender As Object)
		Value = sender.Value
		sender.Parent.Close()
		RaiseChangeEvent()
	End Sub

End Class

Public Class Range

	Dim Dates(2) As Date

	Default Property Value(Index As Integer) As Date
		Get
			Return Dates(Index)
		End Get
		Set(Value As Date)
			Dates(Index) = Value
		End Set
	End Property

End Class